home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
SGI Developer Toolbox 6.1
/
SGI Developer Toolbox 6.1 - Disc 4.iso
/
src
/
demos
/
GL
/
newton
/
cwd.c
< prev
next >
Wrap
C/C++ Source or Header
|
1994-08-01
|
3KB
|
110 lines
/*
* Copyright 1991, 1992, 1993, 1994, Silicon Graphics, Inc.
* All Rights Reserved.
*
* This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
* the contents of this file may not be disclosed to third parties, copied or
* duplicated in any form, in whole or in part, without the prior written
* permission of Silicon Graphics, Inc.
*
* RESTRICTED RIGHTS LEGEND:
* Use, duplication or disclosure by the Government is subject to restrictions
* as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
* and Computer Software clause at DFARS 252.227-7013, and/or in similar or
* successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
* rights reserved under the Copyright Laws of the United States.
*/
/*
* cwd() -- return a pointer to a static string containing the current
* working directory.
*/
#include <stdio.h>
#include <sys/param.h>
#include <sys/signal.h>
#include <sys/types.h>
#include <sys/sysmacros.h>
#include <sys/stat.h>
#include <dirent.h>
static struct stat d, dd;
static struct dirent *dir;
static char dot[] = ".";
static char dotdot[] = "..";
static char name[MAXNAMLEN];
static int off;
static DIR *file;
static void cat(char *new_name);
char *
cwd()
{
off = -1;
for(;;) {
if(stat(dot, &d) < 0) {
fprintf(stderr, "cwd: Cannot stat .!\n");
my_exit(2);
}
if ((file = opendir(dotdot)) == NULL) {
fprintf(stderr,"cwd: Cannot open ..\n");
my_exit(2);
}
if(fstat(file->dd_fd, &dd) < 0) {
fprintf(stderr, "cwd: Cannot stat ..!\n");
my_exit(2);
}
if(chdir(dotdot) < 0) {
fprintf(stderr, "cwd: Cannot chdir to ..\n");
my_exit(2);
}
if(d.st_dev == dd.st_dev) {
if(d.st_ino == dd.st_ino) {
cat("");
if (off < 0)
off = 0;
name[off] = '\0';
chdir(name);
return(name);
}
do
if ((dir = readdir(file)) == NULL) {
fprintf(stderr, "cwd: Read error in ..\n");
my_exit(2);
}
while (dir->d_ino != d.st_ino);
}
else do {
if((dir = readdir(file)) == NULL) {
fprintf(stderr, "cwd: Read error in ..\n");
my_exit(2);
}
stat(dir->d_name, &dd);
} while(dd.st_ino != d.st_ino || dd.st_dev != d.st_dev);
(void)closedir(file);
cat(dir->d_name);
}
}
static void cat(char *new_name)
{
register i, j;
i = -1;
while (new_name[++i] != 0)
if ((off+i+2) > MAXNAMLEN - 1) {
fprintf(stderr, "cwd: Pathname too long\n");
my_exit(2);
}
for(j=off+1; j>=0; --j)
name[j+i+1] = name[j];
off=i+off+1;
name[i] = '/';
for(--i; i>=0; --i)
name[i] = new_name[i];
}